package cn.cqie.attendance.common.util;

import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;
import java.util.List;

/**
 * @author wqh
 * @date 2021/7/13 11:39
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class PageBean<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 200                    // 成功
     * 500                    // 后台发生异常
     * 201                    // 用户信息错误
     * 401                    // 未授权
     * 999                    // 失败
     */
    private Integer code;
    /**
     * 成功使用 'success'
     * 失败用  'fail'
     * 自定义其他提示信息 ‘XXX’
     */
    private String msg;
    private Integer pageNum = 0;   //  当前页
    private Integer pageSize = 10;  //  每页显示记录数
    private Integer total_pages;      //  总页数
    private long records;       //  总记录数
    private List<T> data;       //返回数据列表

    public PageBean(Integer code, List data) {
        PageInfo<List> info = new PageInfo<>(data);
        this.code = code;
        this.records = info.getTotal();
        this.total_pages = info.getPages();
        this.pageNum = info.getPageNum();
        this.pageSize = info.getPageSize();
        this.data = data;
    }

    public PageBean(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public PageBean(Integer code, String msg, List data) {
        PageInfo<List> info = new PageInfo<>(data);
        this.code = code;
        this.msg = msg;
        this.records = info.getTotal();
        this.total_pages = info.getPages();
        this.pageNum = info.getPageNum();
        this.pageSize = info.getPageSize();
        this.data = data;
    }
}
